package xby.study.proxy.cglib.lazyloader;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.LazyLoader;
import xby.study.proxy.cglib.PathUtil;

public class LazyLoaderTest {
    public void f() {
        System.out.println("...f()...");
    }

    public void g() {
        System.out.println("...g()...");
    }

    public void h() {
        System.out.println("...h()...");
    }

    public static void main(String[] args) {
        // 配置class文件的输出目录
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, PathUtil.getOutPath(LazyLoaderTest.class));

        /*
        * LazyLoader 可以看做是对 dispatcher 的优化
        * */
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(LazyLoaderTest.class);
        enhancer.setCallback((LazyLoader) SonLazyLoaderTest::new);
        LazyLoaderTest test = (LazyLoaderTest) enhancer.create();
        test.f();
        test.g();
        test.h();
    }
}

class SonLazyLoaderTest extends LazyLoaderTest {
    public void f() {
        System.out.println("before method  f...");
        System.out.println(this.getClass().getName());
        System.out.println("end method  f..");
    }

    public void g() {
        System.out.println("before method g...");
        System.out.println(this.getClass().getName());
        System.out.println("end method g...");
    }

    public void h() {
        System.out.println("before method h...");
        System.out.println(this.getClass().getName());
        System.out.println("end method h...");
    }
}
